package sdk.webview.fmc.com.fmcsdk.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;

import java.io.File;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.base.BaseActivity;
import sdk.webview.fmc.com.fmcsdk.base.BasePresenter;
import sdk.webview.fmc.com.fmcsdk.bean.RecordError;
import sdk.webview.fmc.com.fmcsdk.record.JCameraView;
import sdk.webview.fmc.com.fmcsdk.record.listener.ClickListener;
import sdk.webview.fmc.com.fmcsdk.record.listener.ErrorListener;
import sdk.webview.fmc.com.fmcsdk.record.listener.JCameraListener;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.SdkVersionUtils;
import sdk.webview.fmc.com.fmcsdk.util.XLog;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.subscriber.RxBus;

/**
 * Created by ydm on 2020/1/6.
 */

public class RecordVideoActivity extends BaseActivity {

    @Override
    protected int getLayoutId() {
        return R.layout.record_video;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

//1.1.1
        JCameraView jCameraView = (JCameraView) findViewById(R.id.jcameraview);
//        String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/fmc/" + PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.APP_NAME, "fmsdk")+"/H5video";
        String storePath;
        if (SdkVersionUtils.checkedAndroid_R()) {
            storePath = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/fmc/" + PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.APP_NAME, "fmsdk") + "/H5video";
        } else {
            storePath = context.getFilesDir().getAbsolutePath() + "/fmc/" + PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.APP_NAME, "fmsdk") + "/H5video";

        }

        File file = new File(storePath);
        if (!file.exists()) {
            file.mkdirs();
        }
//设置视频保存路径
        jCameraView.setSaveVideoPath(storePath);

//设置只能录像或只能拍照或两种都可以（默认两种都可以）
        jCameraView.setFeatures(JCameraView.BUTTON_STATE_ONLY_RECORDER);

//设置视频质量
        jCameraView.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE);
        jCameraView.setErrorLisenter(new ErrorListener() {
            @Override
            public void onError() {
                XLog.i("open camera error");
            }

            @Override
            public void AudioPermissionError() {
                XLog.i("AudioPermissionError");
            }
        });

        jCameraView.setJCameraLisenter(new JCameraListener() {
            @Override
            public void captureSuccess(Bitmap bitmap) {
                XLog.i("bitmap = " + bitmap.getWidth());
            }

            @Override
            public void recordSuccess(String url, Bitmap firstFrame) {
                //获取视频路径
                XLog.i("url = " + url);
                //返回键
                Intent intent = new Intent();
                intent.putExtra(Constant.RECORD_VIDEO_OK, url);
                setResult(0, intent);
                finish();


            }
        });
        //左边按钮点击事件
        jCameraView.setLeftClickListener(new ClickListener() {
            @Override
            public void onClick() {
                finish();
            }
        });
        //左边按钮点击事件
        jCameraView.setRightClickListener(new ClickListener() {
            @Override
            public void onClick() {

            }
        });
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
